const mixin = {
  methods: {
    refresh() {
      this.$refs.dataGrid.refresh()
    },
    importAssetExcel() {
      // @ts-ignore
      this.$refs.importFile.click()
    },
    onImportFileChange() {
      let uploadFile = this.$refs.importFile.files[0]
      this.$loading('导入中...')
      this.import(uploadFile)
          .then(response => {
            // check if upload success
            this.$loading.close()
            let result = response.data.result
            if (result.error == false) {
              this.$msgBox('导入成功')
              // reload all meta
              this.bootstrap().then(this.refresh)
            } else {
              let errors = JSON.parse(result.message)
              let messages = errors.map(x => x[0]).join('\n')
              this.$msgBox(messages)
            }
          })
          .finally(() => {
            this.$refs.importFile.value = null
            this.$loading.close()
          })
    },
    exportAssetExcelTemplate() {
      this.$loading('下载中...')
      this.downloadTemplate()
          .then(response => {
            const url = window.URL.createObjectURL(new Blob([response.data]))
            const link = document.createElement('a')
            link.href = url
            link.setAttribute('download', 'assets.xlsx')
            document.body.appendChild(link)
            link.click()
          })
          .finally(() => {
            this.$loading.close()
          })
    },
    exportDataAsExcel() {
      this.$loading('下载中...')
      this.downloadExcel()
          .then(response => {
            const url = window.URL.createObjectURL(new Blob([response.data]))
            const link = document.createElement('a')
            link.href = url
            link.setAttribute('download', 'assets.xlsx')
            document.body.appendChild(link)
            link.click()
          })
          .finally(() => {
            this.$loading.close()
          })
    }
  }

}

export default mixin